<template>
  <div class="salesMargin">
    <div class="header">
      <div class="header-name">
        <search :options="selectData" :msg="tacitly" @seek="collect" @hand="handleQuerySonClass" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">搜索</el-button>
        <el-button type="primary" size="mini" style="margin-left: 0" @click="handleExport">导出Excel</el-button>
        <filtration
          :ftype="ftype"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        :get-summaries="getSummaries"
        show-summary
      />
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        :page-sizes="[20,200,500,1000,2000,5000,10000]"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>

import {
  querySalGrossList
} from '@/api/financial/salesMargin'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
export default {
  name: 'SalesMargin',
  components: {
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 17,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader1: [],
      tableHeader: [
        { label: this.$t('market.billNo'), prop: '单据编号', minWidth: '130px' },
        { label: this.$t('market.fdate'), prop: '日期', minWidth: '110px' },
        { label: this.$t('market.businessType'), prop: '业务类型', width: '50' },
        { label: this.$t('market.classification'), prop: '分类', width: '50' },
        { label: this.$t('market.cust'), prop: '客户' },
        { label: this.$t('market.paezText1'), prop: '客户订单号', minWidth: '110px' },
        { label: this.$t('market.model'), prop: '型号' },
        { label: this.$t('market.fmaterial'), prop: '物料编码', minWidth: '130' },
        { label: this.$t('shop.fname2'), prop: '物料名称' },
        { label: this.$t('market.productDesc'), prop: '产品描述' },
        { label: this.$t('market.specificaTion'), prop: '规格尺寸' },
        { label: this.$t('market.stockUnit'), prop: '库存单位', width: '50' },
        { label: this.$t('market.realqty'), prop: '实发数量', width: '50' },
        { label: this.$t('market.ftaxPrice'), prop: '含税单价' },
        { label: '收入-原币(金额)', prop: '收入-原币(金额)' },
        { label: this.$t('market.salCurrency'), prop: '销售币别' },
        { label: this.$t('market.rate'), prop: '本位币与人民币汇率' },
        { label: this.$t('market.IncomeRmb'), prop: '收入-人民币' },
        { label: this.$t('market.discou'), prop: '折扣比例' },
        { label: this.$t('market.amountOf') + '（USD)', prop: '折扣金额（USD)' },
        { label: this.$t('market.amountOf') + '（RMB)', prop: '折扣金额（RMB)' },
        { label: this.$t('market.netIncome') + '(RMB)', prop: '净收入(RMB)' },
        { label: this.$t('market.unitCost') + 'RMB', prop: '单位成本(不含税)RMB' },
        { label: this.$t('market.costofAmount') + 'RMB', prop: '成本金额RMB' },
        { label: this.$t('market.singlePro') + 'CBM', prop: '单个产品CBM' },
        { label: this.$t('market.packQty'), prop: '包装方式' },
        { label: this.$t('market.casePack'), prop: '装箱数' },
        { label: 'CBM', prop: 'CBM' },
        { label: 'FOB/PCS', prop: 'FOB/PCS' },
        { label: this.$t('market.gold'), prop: '佣金比例' },
        { label: 'FOB', prop: 'FOB费' },
        { label: this.$t('market.commission'), prop: '佣金' },
        { label: this.$t('market.grossProfit'), prop: '毛利' },
        { label: this.$t('market.mll'), prop: '毛利率' },
        { label: this.$t('market.saler'), prop: '销售员' },
        { label: this.$t('market.saleorg'), prop: '销售组织' },
        { label: this.$t('market.custGroup'), prop: '客户分组' },
        { label: this.$t('market.paezText'), prop: '柜型' },
        { label: this.$t('market.note2'), prop: '行备注' },
        { label: this.$t('market.headerNote'), prop: '表头备注' },
        { label: this.$t('market.storage'), prop: '采购入库单号' },
        { label: this.$t('market.suppliername'), prop: '供应商名称' },
        { label: this.$t('market.actualAmount') + 'RMB', prop: '实发采购金额RMB' },
        { label: this.$t('market.storageInPrice') + 'RMB', prop: '采购入库单价RMB1' }
      ]
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          querySalGrossList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '销售毛利列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取销售毛利列表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        // '日期': ''
        search: this.searCollData.concat(this.details)
      }
      const { data: RES, data: total, code, message } = await querySalGrossList(DATA)
      if (code !== 0) {
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        return
      }
      this.tableData = RES.array
      // 当 tableData 中没有某字段是, 删除 tableHeader 中的某数据
      this.tableData.forEach(item => {
        this.tableHeader.map((ite, index) => {
          if (item[ite.prop] === undefined) {
            this.tableHeader.splice(index, 1)
          }
        })
      })
      this.total = total.total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.salesMargin {
  @include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 23.4vw !important;
    }
  }
}
</style>
